package com.caishi.lkx.course.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.common.role.Roles;
import com.caishi.lkx.course.model.VideoHandoutModel;
import com.caishi.lkx.course.service.IVideoHandoutService;
import com.zzw.common.Wrappers;
import com.zzw.common.entity.impl.BaseEntity;
import com.zzw.common.mysql.config.MybatisPlusContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author lkx
 * &#064;date  2024/5/28
 * &#064;description
 */
@Slf4j
@Tag(name = "视频资料相关接口")
@RestController
@RequestMapping("/kc/course/video/handout")
public class VideoHandoutApi extends AdminBaseApi<IVideoHandoutService, VideoHandoutModel, String> {


    @Resource
    private IVideoHandoutService videoHandoutService;



    @Override
    public IVideoHandoutService loadService() {
        return videoHandoutService;
    }
    @Operation(summary = "根据视频id获取视频资料")
    @GetMapping("list-by-video")
    public List<VideoHandoutModel> listByVideo(@RequestParam String videoId){
            return loadService().selectList(new LambdaQueryWrapper<VideoHandoutModel>().eq(VideoHandoutModel::getVideoId, videoId).orderByDesc(VideoHandoutModel::getDownloadCount));
    }

    @PutMapping("add-frequency")
    @Operation(summary = "增加下载次数")
    @Parameters({
            @Parameter(name = "id", description = "资料id")})
    @Roles
    public void addFrequency(@RequestParam String id) {
        MybatisPlusContext.setContext(new MybatisPlusContext.Context().setNoUpdateModifyTime(true));
        videoHandoutService.update(null, Wrappers.<VideoHandoutModel>lambdaUpdate().eq(BaseEntity::getId, id).setSql("download_count=download_count+1"));
    }
}
